﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.OrderService;
using Newtonsoft.Json;
using RN.ERP.Web.PersonService;
using System.Text;

namespace RN.ERP.Web.CA
{
    public partial class CAOrderInfo : BasePage
    {
        OrderServiceClient client = new OrderServiceClient();
        public string CAStatus = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            string guid = Request.QueryString["guid"];//订单guid
            hidGroup.Value = Request.QueryString["group"];
            ModelService.CaPayInfo caPayInfo = JsonConvert.DeserializeObject<ModelService.CaPayInfo>(client.GetOrderInfo(guid));//获取订单信息
            CAStatus = caPayInfo.bCAStatus.ToString();
            hidUserType.Value = UserInfo.RoleType;
            hidRoleID.Value = UserInfo.RoleId;
            ////list_msg.html
            if (!IsPostBack)
            {
                if (caPayInfo != null)
                {
                    var caPaySub = caPayInfo.PayInfoSub;
                    List<ModelService.CaPayInfoSub> corpList = caPaySub.Where(c => c.bCAKind.Contains("E") || c.bCAKind.Contains("C")).ToList();//企业订单
                    List<ModelService.CaPayInfoSub> personList = caPaySub.Where(c => c.bCAKind.Contains("F") || c.bCAKind.Contains("D")).ToList();//人员订单
                    if (corpList.Count == 0)
                    {
                        tbCorp.Visible = false;
                    }
                    else
                    {
                        repCorp.DataSource = corpList;
                        repCorp.DataBind();
                    }
                    if (personList.Count == 0)
                    {
                        tbPerson.Visible = false;
                    }
                    else
                    {
                        repPerson.DataSource = personList;
                        repPerson.DataBind();
                    }
                }
                else
                {
                    tbCorp.Visible = false;
                    tbPerson.Visible = false;
                }
            }
        }

        protected void repCorp_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.Equals("Corp", StringComparison.OrdinalIgnoreCase))
            {
                if (CAStatus != "0")
                {
                    Guid guid = Guid.Empty;
                    if (Guid.TryParse(UserInfo.CorpUserGuid, out guid))
                    {
                        byte[] bytes = client.GetCorpCAPdf(guid);
                        DownPdf(bytes);
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('下载失败！');", true);
                        //RadWindowManager1.RadAlert("下载失败！", 300, 150, "提示", "");
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('请先提交订单，再下载数字证书申请表！');", true);
                    //RadWindowManager1.RadAlert("请先提交订单，再下载数字证书申请表！", 300, 150, "提示", "");
                }
            }
        }

        protected void repPerson_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (CAStatus != "0")
            {
                PersonServiceClient person = new PersonServiceClient();
                var emp = JsonConvert.DeserializeObject<ModelService.tb_Emp_Manager>(person.GetPersonInfo(e.CommandArgument.ToString(), UserInfo.CurrentArea));
                if (emp != null)
                {
                    if (e.CommandName.Equals("Person1", StringComparison.OrdinalIgnoreCase))
                    {
                        byte[] bytes = client.GetPsnCAPdf(emp.EmpGuid);
                        DownPdf(bytes);
                    }
                    else if (e.CommandName.Equals("Person2", StringComparison.OrdinalIgnoreCase))
                    {
                        byte[] bytes = client.GetPsnKeyPdf(emp.EmpGuid);
                        DownPdf(bytes);
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('下载失败！');", true);
                    //RadWindowManager1.RadAlert("下载失败！", 300, 150, "提示", "");
                }

            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('请先提交订单，再下载数字证书申请表！');", true);
                //RadWindowManager1.RadAlert("请先提交订单，再下载数字证书申请表！", 300, 150, "提示", "");
            }
        }

        private void DownPdf(byte[] bytes)
        {
            try
            {
                if (bytes != null)
                {
                    Response.Clear();
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.AddHeader("Content-Disposition", "attachment;filename=FlyCAUnitApp.PDF");
                    Response.ContentType = "application/pdf";
                    Response.OutputStream.Write(bytes, 0, bytes.Length);
                    Response.OutputStream.Flush();
                    Response.OutputStream.Close();
                    Response.Flush();
                    Response.End();
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('数据校验失败，请检查后再次验证！');", true);
                    //RadWindowManager1.RadAlert("数据校验失败，请检查后再次验证！", 300, 150, "提示", "");
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('下载失败:" + ex.Message + "');", true);
                //RadWindowManager1.RadAlert("下载失败:" + ex.Message, 300, 150, "提示", "");
            }
        }
    }
}